<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务员id" prop="workerId">
        <el-input
          v-model="queryParams.workerId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资id" prop="supplyId">
        <el-select v-model="queryParams.supplyId" placeholder="请选择物资" >
               <el-option
              v-for="supply in supplyList"
              :key="supply.supplyId"
              :label="supply.supplyType"
              :value="supply.supplyId"
            />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务id" align="center" prop="taskId" v-if="true"/>
      <el-table-column label="服务员id" align="center" prop="workerId" />
      <el-table-column label="物资id" align="center" prop="supplyId" />
      <el-table-column label="物资数量" align="center" prop="supplyAmount" />
      <el-table-column label="任务内容" align="center" prop="taskContent" />
      <el-table-column label="任务情况" align="center" prop="isDone" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRate(scope.row)"
          >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCancel(scope.row)"
          >取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />




    <el-dialog :visible.sync="open" title="填写任务具体情况">
      <el-form :model="form" ref="form" label-width="100px">
        <el-form-item label="选择服务需求" prop="serviceRequestId">
          <el-select v-model="form.serviceRequestId" placeholder="请选择服务需求">
            <el-option
              v-for="serviceRequest in serviceRequestList"
              :key="serviceRequest.serviceRequestId"
              :label="serviceRequest.serviceRequestContent"
              :value="serviceRequest.serviceRequestId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择服务人员" prop="workerId">
          <el-select v-model="form.workerId" placeholder="请选择服务人员">
            <el-option
              v-for="worker in workerList"
              :key="worker.userId"
              :label="worker.userName"
              :value="worker.userId"
            />
          </el-select>
        </el-form-item>
         <el-form-item label="填写服务详情" prop="taskContent">
            <el-input v-model="form.taskContent" placeholder="请输入" />
         </el-form-item>
         <el-form-item label="提供的物资" prop="supplyId">
            <el-select v-model="form.supplyId" placeholder="请选择物资" >
               <el-option
              v-for="supply in supplyList"
              :key="supply.supplyId"
              :label="supply.supplyType"
              :value="supply.supplyId"
            />
            </el-select>
         </el-form-item>
         <el-form-item label="物资数量" prop="supplyAmount">
            <el-input v-model="form.supplyAmount" placeholder="请输入" />
         </el-form-item>
         
        <el-row>
            <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </el-row>
      </el-form>
    </el-dialog>


     <el-dialog :visible.sync="rateOpen" title="请对该任务完成程度评分">
      
        <el-form :model="rateForm" ref="rateFrom" label-width="100px">
          
          <el-form-item label="任务评分" prop="rate">
            <el-rate v-model="rateForm.rate" />
          </el-form-item>
         
          <el-form-item label="详细评价" prop="rateContent">
            <el-input v-model="rateForm.rateContent"/>
          </el-form-item>

          <el-form-item label="工资变化" prop="salaryChange">
            <el-input v-model="rateForm.salaryChange" placeholder="请输入数字"/>
          </el-form-item>

          <el-row>
              <el-button :loading="buttonLoading" type="primary" @click="submitRate">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
          </el-row>
        </el-form>
     </el-dialog>



  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask,getMyTaskSent,getWorker,getListNow,getSupply,sendTask,workRate,taskCancel } from "@/api/hotel/task";

export default {
  name: "Task",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      taskList: [],
      serviceRequestList: [],
      workerList:[],
      supplyList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rateOpen: false, 
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workerId: undefined,
        supplyId: undefined,
        supplyAmount: undefined,
        taskContent: undefined,
        serviceRequestId: undefined
      },
      // 表单参数
      form: {},
      rateForm:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    getServiceRequest(){
      getListNow().then(response => {
        this.serviceRequestList = response.rows;
      });
    },

    getWorker(){
      getWorker().then(response => {
        this.workerList = response.rows;
      });
    },

    getSupplyList(){
      getSupply().then(response => {
        this.supplyList = response.rows;
      });
    },



    /** 查询【请填写功能名称】列表 */
    getList() {

      this.getServiceRequest();
      this.getWorker();
      this.getSupplyList()
      
      getMyTaskSent(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      

    },
    // 取消按钮
    cancel() {
      this.rateOpen =false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
       taskId: undefined,
        workerId: undefined,
        supplyId: undefined,
        supplyAmount: undefined,
        taskContent: undefined,
        serviceRequestId: undefined
      };
      this.rateForm={
        rate:undefined,
        workerId:undefined,
        rateContent:undefined,
        taskId:undefined

      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 评分按钮 */
    handleRate(row) {

      if(row.isDone=="y"){
        this.rateForm.workerId=row.workerId;
        this.rateForm.taskId=row.taskId;
        this.rateOpen=true;
      }
      else{
        this.$modal.confirm("任务不需要检查");
      }

    },
    /** 提交按钮 */
    submitForm() {
      this.buttonLoading = true;
            sendTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
    },

//提交评分
    submitRate(){

        this.buttonLoading = true;
              workRate(this.rateForm).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
                this.rateOpen=false;
              });

    },

//任务取消
    handleCancel(row){
      if(row.isDone=="n"){
        taskCancel(row).then(response => {
                this.$modal.msgSuccess("取消成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
      }
      else{
        this.$modal.confirm("任务已处于不能取消的阶段");
      }
    },

    statusFormat(row,column){
      if(row.isDone=="n"){
        return "未完成";
      }
      else if(row.isDone=="y"){
        return "已完成待检查";
      }
      else if(row.isDone=="q"){
        return "已取消";
      }
      else if(row.isDone=="d"){
        return "已检查";
      }
    }

  }
};
</script>
